473,465 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dependent dropdown list with mysql

16 New Member
Hi,
I have two dynamic drop down lists: State and City.
What I'm trying to do is first I need the city list to be empty on load. Then after a visitors selects the "state" from the first list, the page reloads and it shows all the available cities from my database where state is selected.
Here is what I have sofar:

Expand|Select|Wrap|Line Numbers
  1. <?php require_once('Connections/myConnection.php'); ?>
  2. <?php
  3. // Check for errors
  4. error_reporting(E_ALL);
  5. ini_set('display_errors', True);
  6.  
  7. //
  8.  
  9. if (!function_exists("GetSQLValueString")) {
  10. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
  11. {
  12.   $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  13.  
  14.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  15.  
  16.   switch ($theType) {
  17.     case "text":
  18.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  19.       break;    
  20.     case "long":
  21.     case "int":
  22.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  23.       break;
  24.     case "double":
  25.       $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
  26.       break;
  27.     case "date":
  28.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  29.       break;
  30.     case "defined":
  31.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  32.       break;
  33.   }
  34.   return $theValue;
  35. }
  36. }
  37.  
  38. mysql_select_db($database_myConnection, $myConnection);
  39. $query_state_recordset = "SELECT DISTINCT `State` FROM programs ORDER BY `State` ASC";
  40. $state_recordset = mysql_query($query_state_recordset, $myConnection) or die(mysql_error());
  41. $row_state_recordset = mysql_fetch_assoc($state_recordset);
  42. $totalRows_state_recordset = mysql_num_rows($state_recordset);
  43.  
  44. mysql_select_db($database_myConnection, $myConnection);
  45. $query_city_recordset = "SELECT DISTINCT City FROM programs ORDER BY City ASC";
  46. $city_recordset = mysql_query($query_city_recordset, $myConnection) or die(mysql_error());
  47. $row_city_recordset = mysql_fetch_assoc($city_recordset);
  48. $totalRows_city_recordset = mysql_num_rows($city_recordset);
  49. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  50. <html xmlns="http://www.w3.org/1999/xhtml">
  51. <head>
  52. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  53. <title>Untitled Document</title>
  54. </head>
  55.  
  56. <body>
  57. <form id="form1" name="form1" method="post" action="">
  58.   <label></label>
  59.   <p>
  60.     <label>State
  61.     <select name="state_id" id="state_id">
  62.       <option value=""></option>
  63.       <?php
  64. do {  
  65. ?>
  66.       <option value="<?php echo $row_state_recordset['State']?>"><?php echo $row_state_recordset['State']?></option>
  67.       <?php
  68. } while ($row_state_recordset = mysql_fetch_assoc($state_recordset));
  69.   $rows = mysql_num_rows($state_recordset);
  70.   if($rows > 0) {
  71.       mysql_data_seek($state_recordset, 0);
  72.       $row_state_recordset = mysql_fetch_assoc($state_recordset);
  73.   }
  74. ?>
  75.         </select>
  76.     </label>
  77.   </p>
  78.   <p>
  79.     <label>City
  80.     <select name="city_ig" id="city_ig">
  81.       <option value=""></option>
  82.       <?php
  83. do {  
  84. ?>
  85.       <option value="<?php echo $row_city_recordset['City']?>"><?php echo $row_city_recordset['City']?></option>
  86.       <?php
  87. } while ($row_city_recordset = mysql_fetch_assoc($city_recordset));
  88.   $rows = mysql_num_rows($city_recordset);
  89.   if($rows > 0) {
  90.       mysql_data_seek($city_recordset, 0);
  91.       $row_city_recordset = mysql_fetch_assoc($city_recordset);
  92.   }
  93. ?>
  94.         </select>
  95.     </label>
  96.   </p>
  97. </form>
  98. </body>
  99. </html>
  100. <?php
  101. mysql_free_result($state_recordset);
  102.  
  103. mysql_free_result($city_recordset);
  104. ?>
  105.  
The page can be found HERE

Thanks
Sep 15 '07 #1
2 11067
ak1dnar
1,584 Recognized Expert Top Contributor
There were some threads for this. Please use site search tool to find them. Search in "Php forum" with keywords "dependent drop down"
Good Luck!
-ajaxrand
Sep 15 '07 #2
ak1dnar
1,584 Recognized Expert Top Contributor
[Refresh and filter record set]
Thread Title changed to better describe the problem scenario.
Please read here: Use a Good Thread Title
Sep 15 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tony Grider | last post by:
I am trying to find a way to create dependent dropdown lists in C# The old way I knew involved javascript and will no longer work in the new design. I need for each list to be database driven (data...
1
by: Hifni Shahzard | last post by:
Hi, I'm using VS.NET 2003, SQL Server 2000, Windows xp professional; My Question: Some of you might seen in some websites where it requests the Country in a dropdown. If the Country selected,...
2
by: computer_angel | last post by:
can someone help me pls.... how to get the value from the mysql when using two drop down that is dependent to the first? For example....(online enrolment) department then the other dropdown...
3
by: riyaz | last post by:
dear mvps i am a beginner for asp.net ,i want to produce the data in the datagrid based upon the selection from the dropdown list. plz give me some code for me to develop this regards...
11
by: eureka | last post by:
Hi All, I'm training in Servlets, JSP and JavaScript, I have a web page in which there's a "StudentName" textbox and below it is a "Names" Dropdown list. Initially the Textbox is empty and...
2
by: sailajapotha | last post by:
I have one javascript code with me,to show parent dropdown as well as child dropdown. <html> <head> <script type="text/javascript" language="javascript"> function setOptions(chosen) { var...
48
by: coool | last post by:
Hi I'm now trying to have a dependent lists in a form my form is a query based form i.e. I fill my MySQL query from this form I have around 30 fields/columns
5
by: Bels | last post by:
I am currently designing a real estate website using Dreamweaver CS3, php, MySQL on a Mac. On it is a 'property search' form that queries the property database. The form consists of 5 dropdown lists....
1
by: kanimozhiz | last post by:
Hi, I am using dropdown lists. What I need is on the selection of one item in one dropdown list, the second dropdown list should get populated accordingly. can anyone tell me how to do this in C#...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.